package cn.ultrarisk.scadmin.plugin.handler;

import org.apache.lucene.LucenePackage;
import org.apache.solr.common.params.CoreAdminParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.core.CoreContainer;
import org.apache.solr.core.SolrCore;
import org.apache.solr.core.SolrInfoMBean;
import org.apache.solr.handler.admin.CoreAdminHandler;
import org.apache.solr.handler.admin.SystemInfoHandler;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;

import java.util.Map;

/**
 * SCAdminHanlder
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/10/20
 * @since V1.0
 */
public class SCAMonitorHanlder extends CoreAdminHandler {

    public SCAMonitorHanlder() {
        super();
    }

    public SCAMonitorHanlder(CoreContainer coreContainer) {
        super(coreContainer);
    }

    @Override
    public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
        SolrParams params = req.getParams();
        if ("monitor".equals(params.get(CoreAdminParams.ACTION))) {
            handleMonitorAction(req, rsp);
        } else {
            super.handleRequestBody(req, rsp);
        }
    }

    public void handleMonitorAction(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
        NamedList<Object> status = new SimpleOrderedMap<Object>();
        NamedList<NamedList<NamedList<Object>>> cache = new SimpleOrderedMap<NamedList<NamedList<Object>>>();
        for (String name : coreContainer.getAllCoreNames()) {
            status.add(name, getCoreStatus(coreContainer, name, true));
            cache.add(name, getCacheStat(name));
        }
        rsp.add("status", status);
        rsp.add("cache", cache);
        rsp.add("lucene", getLuceneInfo());
        rsp.add("jvm", SystemInfoHandler.getJvmInfo());
        rsp.add("system", SystemInfoHandler.getSystemInfo());
    }

    private SimpleOrderedMap<Object> getLuceneInfo() {
        SimpleOrderedMap<Object> info = new SimpleOrderedMap<Object>();

        Package p = SolrCore.class.getPackage();
        info.add("solr-spec-version", p.getSpecificationVersion());
        info.add("solr-impl-version", p.getImplementationVersion());

        p = LucenePackage.class.getPackage();
        info.add("lucene-spec-version", p.getSpecificationVersion());
        info.add("lucene-impl-version", p.getImplementationVersion());

        return info;
    }

    private NamedList<NamedList<Object>> getCacheStat(String name) {
        NamedList<NamedList<Object>> catInfo = new SimpleOrderedMap<NamedList<Object>>();
        Map<String, SolrInfoMBean> reg = coreContainer.getCore(name).getInfoRegistry();
        for (Map.Entry<String, SolrInfoMBean> entry : reg.entrySet()) {
            String key = entry.getKey();
            SolrInfoMBean m = entry.getValue();
            if (Category.CACHE == m.getCategory()) {
                NamedList<Object> mBeanInfo = new SimpleOrderedMap<Object>();
                mBeanInfo.add("class", m.getName());
                mBeanInfo.add("version", m.getVersion());
                mBeanInfo.add("description", m.getDescription());
                mBeanInfo.add("stats", m.getStatistics());
                catInfo.add(key, mBeanInfo);
            }
        }
        return catInfo;
    }
}
